package bud.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.RequestContextUtils;

import bud.vo.BoardFormDTO;
import bud.vo.BoardshopDTO;
import bud.web.boardshop.service.IBoardshopService;
import bud.web.boardshop.service.IBoardshopService.BoardshopResult;


@Controller
public class BoardshopController {
	@Inject
	IBoardshopService service;
	public IBoardshopService getService() {
		return service;
	}


	public void setService(IBoardshopService service) {
		this.service = service;
	}

	
	@RequestMapping("boardshopList")
	public String boardshopList(String msg, Model model, 
			@ModelAttribute("formDTO") BoardFormDTO<BoardshopDTO> formDTO) {
		if(formDTO.getCurrentPage()==null) {
			formDTO.setCurrentPage(1);
		}
		formDTO.setScreen_size(15);
		formDTO.setBlock_size(7);
		formDTO.setTotalRecords(service.getBoardshopCount(formDTO));

		//페이징 처리를 위한 어트리뷰트를 스코프에 추가
		List<BoardshopDTO> boardshopList = service.getBoardshopList(formDTO);
		formDTO.setDataList(boardshopList);
		model.addAttribute("msg",msg);
		return "boardshop/boardshopList";
	}
	
	@RequestMapping("boardshopForm")
	public String boardshopForm() {
		return "boardshop/boardshopForm";
	}
	@Inject
	WebApplicationContext context;
	@RequestMapping("boardshopInsert")
	public String boardshopInsert(@ModelAttribute("boardshop")BoardshopDTO boardshop, 
			BindingResult error, Model model) throws UnsupportedEncodingException {
		String goPage=null;
		if(error.hasErrors()) {
			model.addAttribute("msg", "알 수 없는 오류가 발생, 잠시 후 다시 시도해주세요.");
			goPage = "boardshop/boardshopForm";
		} 
		BoardshopResult result = service.createBoardshop(boardshop);
		if(result == BoardshopResult.OK) {
			model.addAttribute("msg", "정상 등록 완료");
			goPage = "redirect:/boardshopList.do";
		} else {
			model.addAttribute("msg", "알 수 없는 오류가 발생, 잠시 후 다시 시도해주세요.");
			goPage = "boardshop/boardshopForm";
		}
		return goPage;
	}
	
	@RequestMapping("boardshopView")
	public String boardshopView(String msg, int bsh_no, HttpServletRequest request,
			Model model) {
		BoardshopDTO boardshop = service.getBoardshop(bsh_no);
		if(boardshop == null) {
			msg="글번호에 해당하는 게시글이 없습니다.";
			return "boardshop/boardshopList";
		}
		model.addAttribute("msg", msg);
		
		//댓글 페이징을 위한 어트리뷰트 추가
		BoardFormDTO<BoardshopDTO> form = new BoardFormDTO<BoardshopDTO>();
		form.setCurrentPage(1);
		form.setScreen_size(10);
		form.setBlock_size(5);
		form.setTotalRecords(service.getRshCount(boardshop.getBsh_no()));
		form.setData(boardshop);
		model.addAttribute("formDTO", form);
		model.addAttribute("boardshop", boardshop);
		return  "boardshop/boardshopView";
	}
	
	@RequestMapping("boardshopEdit")
	public String boardshopEdit(String msg, @RequestParam("bsh_no") int bsh_no,
			Model model) {
		BoardshopDTO boardshop = service.getBoardshop(bsh_no);
		if(boardshop == null) {
			msg="글번호에 해당하는 게시글이 없습니다.";
			return "boardshop/boardshopList";
		}
		model.addAttribute("boardshop", boardshop);
		model.addAttribute("msg", msg);
		return "boardshop/boardshopEdit";
	}
	
	@RequestMapping(value="boardshopUpdate",method=RequestMethod.POST)
	public String boardshopUpdate(@ModelAttribute("boardshop") BoardshopDTO boardshop, 
			Model model) throws IOException{
		BoardshopResult result = service.modifyBoardshop(boardshop);
		String goPage=null;
		String msg=null;
		if(result==BoardshopResult.OK) {
			msg="수정 완료";
			model.addAttribute("msg", msg);
			goPage="redirect:/boardshopView.do?bsh_no="+boardshop.getBsh_no();
		} else if(result==BoardshopResult.INVALIDPASSWORD) {
			msg="비밀번호 오류, 확인 후 다시 시도해주세요.";
			model.addAttribute("msg", msg);
			goPage ="redirect:/boardshopEdit.do?bsh_no="+boardshop.getBsh_no();
		} else {
			msg="알 수 없는 오류, 잠시 후 다시 시도해주세요.";
			model.addAttribute("msg", msg);
			goPage = "boardshop/boardshopEdit.do?bsh_no="+boardshop.getBsh_no();
		}
		return goPage;
	}
	
	@RequestMapping("boardshopDelete") 
	public String boardshopDelete(@ModelAttribute("boardshop") BoardshopDTO boardshop,
			HttpServletResponse response, HttpServletRequest request, Model model) throws IOException {
		if(boardshop.getBsh_no()==null) {
			response.sendError(HttpServletResponse.SC_BAD_REQUEST, "삭제글 번호가 없습니다");
			return null;
		}
		BoardshopResult result = service.removeBoardshop(boardshop);
		String goPage = null;
		String msg = null;
		if(result == BoardshopResult.OK) {
			msg="삭제 완료";
			model.addAttribute("msg", msg);
			goPage = "redirect:/boardshopList.do";
		} else if(result==BoardshopResult.INVALIDPASSWORD) {
			msg="비밀번호 오류, 확인 후 다시 시도해주세요";
			model.addAttribute("msg", msg);
			goPage = "redirect:/boardshopEdit.do?bsh_no="+boardshop.getBsh_no();
		} else {
			msg="알수없는 오류로 정상처리 불가능, 잠시 후 다시 시도해주세요";
			model.addAttribute("msg", msg);
			goPage = "redirect:/boardshopView.do?bsh_no="+boardshop.getBsh_no();
		}
		return goPage;
	}
}


